package java学习.第11章_常用类和基础API;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * ClassName: B_String之构造器
 * Package: java学习.第11章_常用类和基础API
 * Description:
 *
 * @Author 张蛟龙
 * @Create 2023/3/23 18:57
 * @Version 1.0
 */
public class B_String之构造器 {

    @Test
    public void 构造器(){
        String s1 = new String();
        System.out.println(s1);
        String s2 = new String("*----");
        System.out.println(s2);
        String s3 = new String(new char[]{'1','2'});
        System.out.println(s3);
        String s4 = new String(new byte[]{97,57,25});
        System.out.println(s4);

    }
    @Test
    public void String与其他类型转换() throws UnsupportedEncodingException {
        String s1 = "123张";
//        int i1 = Integer.parseInt(s1);
        char[] chars = s1.toCharArray();
        byte[] byte1 = s1.getBytes(StandardCharsets.UTF_8);//使用UTF_8解码
        byte[] byte2 = s1.getBytes();//默认字符集
        byte[] byte3 = s1.getBytes("gbk");//默认字符集
        for (int i = 0; i < byte3.length; i++) {
            System.out.println(byte3[i]);
        }
        String s2 = new String(byte3,StandardCharsets.UTF_8);//使用UTF_8 加码

    }
}
